/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getScanRecordsList,
  getScanTypesList,
  addScanType,
  deleteScanType,
  getScanOptionList,
  getScanType,
} from '@/services/allocation';
import { isLegalData } from '@/utils/utils';
import { message, Modal } from 'antd';

export default {
  namespace: 'scan',

  state: {
    scanRecordsList: {
      list: [],
      pagination: {},
    },
    scanTypesList: [],
    siteNumberList: [],
    scanOptionList: [],
    scanTypeSelectList: [],
  },

  effects: {
    // 自定义扫描，获取扫描记录列表
    *getScanRecordsList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getScanRecordsList, payload);
      const { n_export } = payload;
      if (!response) return;
      const { code, data, msg } = response;
      const { list, total, page } = data;

      if (code == 0 && data) {
        if (n_export) {
          Modal.info({
            title: '温馨提示',
            content: '导出数据申请已提交，请至【报表下载】处下载',
          });
        } else {
          yield put({
            type: 'save',
            payload: {
              scanRecordsList: {
                list: isLegalData(list, []),
                pagination: {
                  total,
                  pageSize: 20,
                  current: page,
                  showQuickJumper: false,
                  showSizeChanger: false,
                },
              },
            },
          });
        }
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 自定义扫描，添加扫描类型
    *addScanType({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addScanType, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 自定义扫描，删除扫描类型
    *deleteScanType({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(deleteScanType, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        __dva_resolve(response);
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 自定义扫描，获取扫描记录列表
    *getScanTypesList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getScanTypesList, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0 && data) {
        yield put({
          type: 'save',
          payload: {
            scanTypesList: isLegalData(data, []),
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 自定义扫描，扫描类型，扫描类型下拉接口
    *getScanOptionList({ payload, __dva_resolve, __dva_reject }, { call, put, select }) {
      const { scanOptionList } = yield select(_ => _.scan);
      if (scanOptionList.length > 0) return;
      const response = yield call(getScanOptionList, payload);
      if (!response || scanOptionList.length > 0) return;
      const { code, data, msg } = response;

      if (code == 0 && data) {
        yield put({
          type: 'save',
          payload: {
            scanOptionList: isLegalData(data, []),
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 自定义扫描，扫描类型，扫描类型下拉接口
    *getScanType({ payload, __dva_resolve, __dva_reject }, { call, put, select }) {
      const { scanTypeSelectList } = yield select(_ => _.scan);
      if (scanTypeSelectList.length > 0) return;
      const response = yield call(getScanType, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0 && data) {
        const list = isLegalData(data, []);
        list.unshift({
          scan_type: '全部',
        });
        yield put({
          type: 'save',
          payload: {
            scanTypeSelectList: list,
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
